package com.senge.cart.listener;

import com.hmall.common.utils.UserContext;
import com.senge.cart.service.ICartService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class DeleteCartItemListener {
    private final ICartService cartService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "cart.clear.queue", durable = "true"),
            exchange = @Exchange(name = "trade.topic"),
            key = "order.create"
    ))
    public void deleteCartItemByIds(Map<String,Object> map){
        Object userIdObj = map.get("userid");
        Long userId = Long.parseLong(userIdObj.toString());
        UserContext.setUser(userId);

        List<Long> ids = (List<Long>) map.get("itemIds");
        cartService.removeByItemIds(ids);
    }
}
